package com.momo.mal.anime.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.DOMException;

import net.momo.mal.R;
import com.momo.mal.entity.Anime;
import com.momo.mal.entity.History;
import com.momo.mal.util.DrawableManager;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HistoryAdapter extends BaseAdapter {
	private List<History> histories = new ArrayList<History>();
	private LayoutInflater mInflater;
	private Activity activity;
	private DrawableManager drawableManager;
	
	public HistoryAdapter(List<History> histories, Activity activity) {
		mInflater = (LayoutInflater) activity
				.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
		
		drawableManager = new DrawableManager(activity);
		this.histories = histories;
		this.activity = activity;
	}

	@Override
	public int getCount() {
		return histories.size();
	}

	@Override
	public Object getItem(int position) {
		return histories.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int location, View view, ViewGroup viewgroup) {
		ViewHolder holder;
		if(view == null) {
			holder = new ViewHolder();
			
			view = mInflater.inflate(
					R.layout.dashboard_history_adapter, null);
			
			holder.title = (TextView) view.findViewById(R.id.text_title);
			holder.updateDate = (TextView) view.findViewById(R.id.text_description);
			holder.description = (TextView) view.findViewById(R.id.text_update_date);
			holder.image = (ImageView) view.findViewById(R.id.image_anime);
			
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		
//		if(location % 2 == 0) {
//			view.setBackgroundColor(Color.WHITE);
//		} else {
//			view.setBackgroundColor(Color.TRANSPARENT);
//		}
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		SimpleDateFormat format2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
		String updateDate = null;
		try {
			updateDate = format2.format(format.parse(histories.get(location).getUpdateDate()));
		} catch (DOMException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		holder.title.setText(histories.get(location).getTitle());
		holder.updateDate.setText(updateDate);
		holder.description.setText(histories.get(location).getDescription());
		
		drawableManager.fetchBitmapOnThread(histories.get(location).getAnimeId().length() != 0 ? 
				"anime_" + histories.get(location).getAnimeId() : "manga_" + histories.get(location).getMangaId(), holder.image);
		
		
		return view;
	}
	
	private class ViewHolder {
		public TextView title;
		public TextView updateDate;
		public TextView description;
		public ImageView image;
	}

}
